package tw.com.guestbook.client;

import java.awt.Dialog;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class PostList implements EntryPoint, ClickHandler {
	private List<Post> posts;
	private FlexTable flexTable;

	@Override
	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
		rootPanel.setStyleName("");

		DockPanel dockPanel = new DockPanel();

		rootPanel.add(dockPanel, 36, 24);
		dockPanel.setSize("361px", "246px");

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		dockPanel.add(horizontalPanel, DockPanel.NORTH);

		Button btnNewButton = new Button("\u65B0\u589E");
		btnNewButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				AddPostDialog addPostDialog = new AddPostDialog();
				addPostDialog.center();
			}
		});
		horizontalPanel.add(btnNewButton);

		Button btnNewButton_1 = new Button("\u4FEE\u6539");
		horizontalPanel.add(btnNewButton_1);

		Button btnNewButton_2 = new Button("\u522A\u9664");
		horizontalPanel.add(btnNewButton_2);

		flexTable = new FlexTable();

		dockPanel.add(flexTable, DockPanel.CENTER);
		flexTable.setSize("361px", "129px");
		flexTable.addClickHandler(this);
		this.readPosts();

	}

	private void readPosts() {
		PostServiceAsync postService = (PostServiceAsync) GWT
				.create(PostService.class);
		postService.getAllPosts(new AsyncCallback<List<Post>>() {

			@Override
			public void onSuccess(List<Post> result) {
				flexTable.clear();
				posts = result;
				int i = 0;
				for (Post post : result) {

					flexTable.setWidget(i, 0, new Label(post.getId() + ""));
					flexTable.setWidget(i, 1,
							new Label(post.getNickname() + ""));
					flexTable.setWidget(i, 2, new Label(post.getTitle() + ""));

					i++;
				}

			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("遠端呼叫失敗");

			}
		});

	}

	public List<Post> getPosts() {
		return posts;
	}

	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}

	@Override
	public void onClick(ClickEvent event) {
		Cell cell = flexTable.getCellForEvent(event);
		Post p = posts.get(cell.getRowIndex());

		// flexTable.getRowFormatter().removeStyleName(selectRow,
		// "selectedRow");

		
		int index=cell.getRowIndex();
		flexTable.getRowFormatter().addStyleName(cell.getRowIndex(),
				"selectedRow");

		Detial detil = new Detial();
		detil.setValue(p);
//		selectRow = cell.getRowIndex();
		DialogBox dialogBox = new DialogBox();
		dialogBox.setAnimationEnabled(true);
		dialogBox.setStyleName("dialog");
		dialogBox.add(detil);
		dialogBox.setAutoHideEnabled(true);
		dialogBox.setGlassEnabled(true);
		dialogBox.center();
	}

}
